package com.daixu.money;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.daixu.money.base.BaseActivity;
import com.daixu.money.home.HomeFragment;
import com.daixu.money.more.MoreFragment;
import com.daixu.money.utils.ToastUtil;

import java.util.List;

@Route(path = "/daixu/main")
public class MainActivity extends BaseActivity implements BottomNavigationBar.OnTabSelectedListener {
    private BottomNavigationBar bottomNavigationBar;

    private HomeFragment mHomeFragment;
    private HomeFragment mHomeFragment2;
    private HomeFragment mHomeFragment3;
    private HomeFragment mHomeFragment4;
    private MoreFragment moreFragment;

    private FragmentManager fm;

    private long mExitTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        fm = getSupportFragmentManager();
        initBottomNavigationBar();

        setDefaultFragment();
    }

    /**
     * 设置默认的
     */
    private void setDefaultFragment() {
        if (mHomeFragment == null) {
            mHomeFragment = HomeFragment.newInstance(getString(R.string.tab_home));
        }
        showFragment(mHomeFragment);
    }

    private void initBottomNavigationBar() {
        bottomNavigationBar = findViewById(R.id.bottomNavigationBar);
        bottomNavigationBar.clearAll();

        bottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED);
        bottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC);

        bottomNavigationBar
                .addItem(new BottomNavigationItem(R.mipmap.ic_launcher, getString(R.string.tab_home)).setActiveColorResource(R.color.main_body_major_color)
                        .setInActiveColorResource(R.color.main_body_prompt_color).setInactiveIconResource(R.mipmap.ic_launcher))
                .addItem(new BottomNavigationItem(R.mipmap.ic_launcher, getString(R.string.tab_account)).setActiveColorResource(R.color.main_body_major_color)
                        .setInActiveColorResource(R.color.main_body_prompt_color).setInactiveIconResource(R.mipmap.ic_launcher))
                .addItem(new BottomNavigationItem(R.mipmap.ic_launcher, getString(R.string.tab_budget)).setActiveColorResource(R.color.main_body_major_color)
                        .setInActiveColorResource(R.color.main_body_prompt_color).setInactiveIconResource(R.mipmap.ic_launcher))
                .addItem(new BottomNavigationItem(R.mipmap.ic_launcher, getString(R.string.tab_report)).setActiveColorResource(R.color.main_body_major_color)
                        .setInActiveColorResource(R.color.main_body_prompt_color)
//                        .setBadgeItem(badgeItem)
                        .setInactiveIconResource(R.mipmap.ic_launcher))
                .addItem(new BottomNavigationItem(R.mipmap.ic_launcher, getString(R.string.tab_more)).setActiveColorResource(R.color.main_body_major_color)
                        .setInActiveColorResource(R.color.main_body_prompt_color).setInactiveIconResource(R.mipmap.ic_launcher))
                .setFirstSelectedPosition(0)
                .initialise();

        bottomNavigationBar.setTabSelectedListener(this);
    }

    @Override
    public void onTabSelected(int position) {
        switch (position) {
            case 0:
                if (mHomeFragment == null) {
                    mHomeFragment = HomeFragment.newInstance(getString(R.string.tab_home));
                }
                showFragment(mHomeFragment);
                break;
            case 1:
                if (mHomeFragment2 == null) {
                    mHomeFragment2 = HomeFragment.newInstance(getString(R.string.tab_account));
                }
                showFragment(mHomeFragment2);
                break;
            case 2:
                if (mHomeFragment3 == null) {
                    mHomeFragment3 = HomeFragment.newInstance(getString(R.string.tab_budget));
                }
                showFragment(mHomeFragment3);
                break;
            case 3:
                if (mHomeFragment4 == null) {
                    mHomeFragment4 = HomeFragment.newInstance(getString(R.string.tab_report));
                }
                showFragment(mHomeFragment4);
                break;
            case 4:
                if (moreFragment == null) {
                    moreFragment = MoreFragment.newInstance(getString(R.string.tab_more));
                }
                showFragment(moreFragment);
                break;
            default:
                break;
        }
        // 事务提交
//        transaction.commit();
    }

    private void showFragment(Fragment fragment) {
        if (!fragment.isVisible()) {
            @SuppressLint("RestrictedApi") List<Fragment> fragList = fm.getFragments();
            FragmentTransaction transaction = fm.beginTransaction();
            if (fragList != null && fragList.contains(fragment)) {
                for (Fragment frag : fragList) {
                    if (frag.equals(fragment) && frag.isHidden()) {
                        transaction.show(frag);
                    } else {
                        transaction.hide(frag);
                    }
                }
                transaction.commitAllowingStateLoss();
            } else {
                if (fragList != null && fragList.size() > 0) {
                    for (Fragment frag : fragList) {
                        transaction.hide(frag);
                    }
                }
                transaction.add(R.id.bottom_nav_content, fragment);
                transaction.commitAllowingStateLoss();
            }
        }
    }

    @Override
    public void onTabUnselected(int position) {
    }

    @Override
    public void onTabReselected(int position) {
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        int time = 2000;
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - mExitTime) > time) {
                ToastUtil.showTip(this, R.string.app_exit_prompt);
                mExitTime = System.currentTimeMillis();
                return true;
            } else {
                finish();
            }
        }
        return super.onKeyDown(keyCode, event);
    }
}
